import { parse } from 'url';
import webpath from '@/utils/webpath';

// mock tableListDataSource
let tableListDataSource = [];
for (let i = 0; i < 46; i += 1) {
    tableListDataSource.push({
        name: `李海波 name ${i}`,
        parentid: `李海波 parentid ${i}`,
        levelcode: `李海波 levelcode ${i}`,
        icon: `李海波 icon ${i}`,
        cls: `李海波 cls ${i}`,
        leaf: `李海波 leaf ${i}`,
        squences: `李海波 squences ${i}`,
        levels: `李海波 levels ${i}`,
        enables: `李海波 enables ${i}`,
        id: `李海波 id ${i}`,
        systime: `李海波 systime ${i}`,
        status: Math.floor(Math.random() * 10) % 4,
    });
}


let areaTree = [];
	areaTree.push(
	  { title: '学校银行', key: '5',org_id:'333',org_code:"00000",org_name:'中国移动' },
      { title: '姜旭聪你想什么呢', key:'00000000',org_id:'4444',org_code:"00000" },
      { title: '电脑电脑', key: '7', isLeaf: true,org_id:'55555',org_code:"00000" },
	);
function getAreaTree(req, res, u) {
    let url = u;
    if (!url || Object.prototype.toString.call(url) !== '[object String]') {
        url = req.url; // eslint-disable-line
    }
    const params = parse(url, true).query;
    let dataSource = areaTree;
    const result = {
        list: dataSource,
    };
    return res.json(result);
}

function getProAreaTreeByParentid (req,res,u,b){
	return getAreaTree(req, res, u)
}

function getArea(req, res, u) {
    let url = u;
    if (!url || Object.prototype.toString.call(url) !== '[object String]') {
        url = req.url; // eslint-disable-line
    }

    const params = parse(url, true).query;

    let dataSource = tableListDataSource;

    if (params.sorter!=undefined) {
        const s = params.sorter.split('_');
        dataSource = dataSource.sort((prev, next) => {
            if (s[1] === 'descend') {
            return next[s[0]] - prev[s[0]];
        }
        return prev[s[0]] - next[s[0]];
    });
    }

    if (params.status!=undefined) {
    const status = params.status.split(',');
    let filterDataSource = [];
    status.forEach(s => {
    filterDataSource = filterDataSource.concat(
    dataSource.filter(data =>
    {

        if(data.status!=undefined){
        return parseInt(data.status, 10) === parseInt(s[0], 10);
        }
    })
    );
    });
    dataSource = filterDataSource;
    }

    if (params.name!=undefined) {
    dataSource = dataSource.filter(data => {

        if(data.name!=undefined){
        return data.name.indexOf(params.name) > -1;
        }

    });
    }

    let pageSize = 10;
    if (params.pageSize!=undefined) {
        pageSize = params.pageSize * 1;
    }

    const result = {
        list: dataSource,
        pagination: {
            total: dataSource.length,
            pageSize,
            current: parseInt(params.currentPage, 10) || 1,
        },
    };

    return res.json(result);
}



function deleteByPrimaryKeyForArea(req, res, u, b) {
    let url = u;
    if (!url || Object.prototype.toString.call(url) !== '[object String]') {
        url = req.url; // eslint-disable-line
    }

    const body = (b && b.body) || req.body;
    const { method, name, age, id } = body;

    if(id!=undefined)
    tableListDataSource = tableListDataSource.filter(item => id.indexOf(item.id) === -1);



    return getArea(req, res, u);
}


function selectByPrimaryKeyForArea(req, res, u, b) {
    let url = u;
    if (!url || Object.prototype.toString.call(url) !== '[object String]') {
        url = req.url; // eslint-disable-line
    }

    const body = (b && b.body) || req.body;
    const { method, name, age, id } = body;


    if(id!=undefined)
    tableListDataSource = tableListDataSource.filter(item => id.indexOf(item.id) === -1);



return getArea(req, res, u);
}



function insertForArea(req, res, u, b) {
    let url = u;
    if (!url || Object.prototype.toString.call(url) !== '[object String]') {
        url = req.url; // eslint-disable-line
    }

    const body = (b && b.body) || req.body;
    const { method, name, age, id } = body;



    const i = Math.ceil(Math.random() * 10000);
    tableListDataSource.unshift({
        name: `李海波 name ${i}`,
        parentid: `李海波 parentid ${i}`,
        levelcode: `李海波 levelcode ${i}`,
        icon: `李海波 icon ${i}`,
        cls: `李海波 cls ${i}`,
        leaf: `李海波 leaf ${i}`,
        squences: `李海波 squences ${i}`,
        levels: `李海波 levels ${i}`,
        enables: `李海波 enables ${i}`,
        id: `李海波 id ${i}`,
        systime: `李海波 systime ${i}`,
        status: Math.floor(Math.random() * 10) % 4,
    });


    return getArea(req, res, u);
}




function updateForArea(req, res, u, b) {
    let url = u;
    if (!url || Object.prototype.toString.call(url) !== '[object String]') {
        url = req.url; // eslint-disable-line
    }

    const body = (b && b.body) || req.body;
    const { method, name, age, id } = body;

    if(id!=undefined)
    tableListDataSource = tableListDataSource.map(item => {
        if (item.id === id) {
        Object.assign(item, { desc, name });
        return item;
    }
    return item;
});


    return getArea(req, res, u);
}


function deleteByMapForArea(req, res, u, b) {
    let url = u;
    if (!url || Object.prototype.toString.call(url) !== '[object String]') {
        url = req.url; // eslint-disable-line
    }

    const body = (b && b.body) || req.body;
    const { method, name, age, id } = body;


    if(id!=undefined)
    tableListDataSource = tableListDataSource.filter(item => id.indexOf(item.id) === -1);



return getArea(req, res, u);
}



function updateByMapForArea(req, res, u, b) {
    let url = u;
    if (!url || Object.prototype.toString.call(url) !== '[object String]') {
        url = req.url; // eslint-disable-line
    }

    const body = (b && b.body) || req.body;
    const { method, name, age, id } = body;


    if(id!=undefined)
    tableListDataSource = tableListDataSource.filter(item => id.indexOf(item.id) === -1);



return getArea(req, res, u);
}

function getOneForArea(req, res, u, b) {
    let url = u;
    if (!url || Object.prototype.toString.call(url) !== '[object String]') {
        url = req.url; // eslint-disable-line
    }

    const body = (b && b.body) || req.body;
    const { method, name, age, id } = body;


    if(undefined!=id)
    tableListDataSource = tableListDataSource.filter(item => id.indexOf(item.id) === -1);



    return getArea(req, res, u);
}

function getAllForArea(req, res, u, b) {
    let url = u;
    if (!url || Object.prototype.toString.call(url) !== '[object String]') {
        url = req.url; // eslint-disable-line
    }

    const body = (b && b.body) || req.body;
    const { method, name, age, id } = body;


    if(id!=undefined)
    tableListDataSource = tableListDataSource.filter(item => id.indexOf(item.id) === -1);



    return getArea(req, res, u);
}

function queryPageForArea(req, res, u, b) {


    return getArea(req, res, u);
}
function updateChangeTargetNodeWithSourceNode(req, res, u, b) {


    return getArea(req, res, u);
}

function countForArea(req, res, u, b) {
    let url = u;
    if (!url || Object.prototype.toString.call(url) !== '[object String]') {
        url = req.url; // eslint-disable-line
    }

    const body = (b && b.body) || req.body;
    const { method, name, age, id } = body;


    if(id!=undefined)
    tableListDataSource = tableListDataSource.filter(item => id.indexOf(item.id) === -1);



return getArea(req, res, u);
}


function insertBatchForArea(req, res, u, b) {
    let url = u;
    if (!url || Object.prototype.toString.call(url) !== '[object String]') {
        url = req.url; // eslint-disable-line
    }

    const body = (b && b.body) || req.body;
    const { method, name, age, id } = body;


    if(id!=undefined)
    tableListDataSource = tableListDataSource.filter(item => id.indexOf(item.id) === -1);



return getArea(req, res, u);
}



function deleteBatchForArea(req, res, u, b) {
    let url = u;
    if (!url || Object.prototype.toString.call(url) !== '[object String]') {
        url = req.url; // eslint-disable-line
    }

    const body = (b && b.body) || req.body;
    const { method, name, age, id } = body;


    if(id!=undefined)
    tableListDataSource = tableListDataSource.filter(item => id.indexOf(item.id) === -1);



return getArea(req, res, u);
}


function updateBatchForArea(req, res, u, b) {
    let url = u;
    if (!url || Object.prototype.toString.call(url) !== '[object String]') {
        url = req.url; // eslint-disable-line
    }

    const body = (b && b.body) || req.body;
    const { method, name, age, id } = body;


    if(id!=undefined)
    tableListDataSource = tableListDataSource.filter(item => id.indexOf(item.id) === -1);



return getArea(req, res, u);
}




export default {
    'GET /${webpath}/web/api/service/area/delete': deleteByPrimaryKeyForArea,
    'GET /${webpath}/web/api/service/area/get': selectByPrimaryKeyForArea,
    'POST /${webpath}/web/api/service/area/insert': insertForArea,
    'POST /${webpath}/web/api/service/area/update': updateForArea,
    'POST /${webpath}/web/api/service/area/deleteByMap': deleteByMapForArea,
    'POST /${webpath}/web/api/service/area/updateByMap': updateByMapForArea,
    'POST /${webpath}/web/api/service/area/getOne': getOneForArea,
    'POST /${webpath}/web/api/service/area/getAll': getAllForArea,
    'POST /${webpath}/web/api/service/area/queryPage': queryPageForArea,
    'POST /${webpath}/web/api/service/area/count': countForArea,
    'POST /${webpath}/web/api/service/area/insertBatch': insertBatchForArea,
    'POST /${webpath}/web/api/service/area/deleteBatch': deleteBatchForArea,
    'POST /${webpath}/web/api/service/area/updateBatch': updateBatchForArea,
    
    
    'POST /${webpath}/web/api/service/area/getAreaTreeByParentid': getProAreaTreeByParentid,
    'POST /${webpath}/web/api/service/area/updateChangeTargetNodeWithSourceNode': updateChangeTargetNodeWithSourceNode,
};
